import React, { useEffect, useState } from 'react';
import { Input, Button, Table, message, Modal, Select } from 'antd';
import './AddDepartment.css'
import axios from 'axios';

export default function AddDepartment() {
    const [department, setDepartment] = useState('');  // 科室名称
    const [doctor, setDoctor] = useState('');  // 科室主任
    const [doctorList, setDoctorList] = useState([]);  // 医生列表
    
    const departmentChange = (e) => {
        setDepartment(e.target.value);
    }
    const doctorChange = (value) => {
        setDoctor(value);
    }
    // 点击添加
    const handleAdd = () => {
        // console.log(department, doctor);
        axios.get('http://8.148.7.123:8000/department/add',{
            params: {
                name: department,
                maindoctor: doctor
            }
        }).then(res=>{
            if(res.data.code === 1){
                message.success(res.data.msg);
                setDepartment('');
                setDoctor('');
            }else if(res.data.code === 20004){
                message.warning('请填写必要信息')
            }else{
                message.error('添加失败')
            }
        })
    }
    // 获取医生列表
    const getDoctorList = () => {
        axios.get('http://8.148.7.123:8000/doctor/doctornames')
        .then(res => {
            setDoctorList(res.data.data.map(item => {
                return {
                    value: item.name,
                    label: item.name,
                }
            }));
        }).catch(err => {
            console.error(err);
        });
    }

    useEffect(() => {
        getDoctorList();
    }, []);
    return <div className="add_box">
        <div>
            <label htmlFor="">科室名称</label>
            <Input value={department} onChange={departmentChange} style={{ width: '400px' }} />
        </div>
        <div>
            <label htmlFor="">科室主任</label>
            <Select style={{ width: '150px' }}
                placeholder='全部医生'
                onChange={doctorChange}
                options={doctorList}
            />
        </div>
        <div>
            <label htmlFor="">科室编号</label>
            <Input disabled placeholder='系统自动生成' style={{ width: '400px' }} />
        </div>
        <div>
            <label htmlFor="">创建时间</label>
            <Input disabled placeholder='系统自动生成' style={{ width: '400px' }} />
        </div>
        <div>
            <Button type='primary' style={{ marginRight: '20px' }} onClick={handleAdd}>确认创建</Button>
            <Button>返回上一级</Button>
        </div>
        
    </div>
}